package cn.tedu.luban.demand.main.service;

import cn.tedu.luban.demand.main.repository.DemandRepository;
import cn.tedu.luban.demand.protocol.bo.RequestOrderBO;
import cn.tedu.luban.demand.protocol.param.GrabOrderParam;
import cn.tedu.luban.demand.protocol.query.RequestOrderQuery;
import com.tedu.inn.protocol.ListRecordTotalBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class DemandService {

    @Autowired
    private DemandRepository demandRepository;

    public ListRecordTotalBO<RequestOrderBO> searchRequestOrders(RequestOrderQuery query) {

        // 1.查询总数
        Long total = demandRepository.getRequestOrderCount(query);

        ListRecordTotalBO<RequestOrderBO> listRecordTotalBO = null;
        // 判断是否有数据
        if(total == 0){ //没有
            // 展示列表(封装)
            listRecordTotalBO = new ListRecordTotalBO<>(null,total);
        }else{ //有

            List<RequestOrderBO> bos = demandRepository.queryRequestOrders(query);
            // 计算展示价格
            calculateViewAmount(bos);
            // 展示列表(封装)
            listRecordTotalBO = new ListRecordTotalBO<>(bos,total);
        }

        return listRecordTotalBO;
    }

    /*
    假设: 原价格:1000 分润比例: 20
     展示价格:ViewOrderAmount:  1000*(100-20) / 100
     */
    private void calculateViewAmount(List<RequestOrderBO> bos) {
        // 分润比例 : 固定20%
        BigDecimal profitScale = new BigDecimal("20");

        //遍历bos,获取每一个bo计算展示价格
        for(RequestOrderBO bo : bos){
            // 获取原始价格
            Long orderAmount = bo.getOrderAmount();
            // 计算订单的分润
            // BigDecimal为100 . 从100中减去20 . 除以100
            BigDecimal workerPercent =
                    new BigDecimal("100")
                            .subtract(profitScale)
                            .divide(new BigDecimal("100"));

            log.info("当前师傅分润:{}",workerPercent);

            // 计算展示价格
            // BigDecimal为 orderAmount(原始价格) . 乘以 workerPercent(分润) . 获取long值
            Long viewOrderAmount =
                    new BigDecimal(orderAmount).multiply(workerPercent).longValue();

            // 缺少属性补齐 展示价格\分润比例
            bo.setViewOrderAmount(viewOrderAmount);
            bo.setProfitScale(profitScale);
        }

    }

    public Boolean grabOrder(String requestOrderNo) {
        // update request_order set grab_status=1 where grab_status=0 and request_order_no=123;
        // 新建重载 grabOrder(String requestOrderNo)
        Integer count  = demandRepository.grabOrder(requestOrderNo);
        return count == 1;

//        GrabOrderParam grabOrderParam = new GrabOrderParam();
//        grabOrderParam.setRequestOrderNO(requestOrderNo);
//        Integer count  = demandRepository.grabOrder(grabOrderParam);
//        return count == 1;

    }

    public Boolean returnOrder(String requestOrderNo) {
        // 方法重载
        Integer count = demandRepository.returnOrder(requestOrderNo);
        return count==1;
    }
}
